<?php

/**
 * 检测验证码
 * @param  integer $id 验证码ID
 * @return boolean     检测结果
 */
function checkVerify($code, $id = 1)
{
    $verify = new \Think\Verify();
    return $verify->check($code, $id);
}

/**
 * 检测管理员用户是否登录
 * @return integer 0-未登录，大于0-当前登录管理员用户ID
 */
function isStoreLogin()
{
    $user = session('store_auth');
    if (empty($user['user_id'])) {
        return 0;
    } else {
        return $user['user_id'];
    }
}

/**
 * 当前管理员管理的店铺ID
 * @return integer
 */
function getStoreID()
{
    $user = session('store_auth');
    if (empty($user['store_id'])) {
        return 0;
    } else {
        return $user['store_id'];
    }    
}

/**
 * 检测当前用户是否为超级管理员
 * @return boolean true，false
 */
function isStoreAdministrator()
{
    $admin = session('store_auth');
    if (empty($admin['is_super'])) {
        return false;
    } else {
        return true;
    }
}

/**
 * module_id
 *
 * @param $controller
 * @param $action
 * @return int
 */
function getModuleID($controller, $action)
{
    if (empty($controller) || empty($action)) {
        return false;
    }
    $where = array();
    $where['status'] = 1;
    $where['module_action'] = strtolower($controller);
    $where['module_name'] = strtolower($action);
    $module = M('UserModule')->where($where)->find();
    return ! empty($module['module_id']) ? $module['module_id'] : false;
}


/**
 * 消息提示，主要适用于普通页面AJAX提交的情况
 *
 * @param string $message 消息内容
 * @param string $url 提示完后的URL去向
 * @param stting $alert_type 提示类型 error/succ/notice 分别为错误/成功/警示
 * @param string $extrajs 扩展JS
 * @param int $time 停留时间
 */
function showDialog($message = '', $url = '', $alert_type = 'error', $extrajs = '', $time = 2)
{
    $extra = '';
    $message = str_replace("'", "\\'", strip_tags($message));
    
    $paramjs = '';
    if ($url == 'reload') {
        $paramjs = 'window.location.reload()';
    } elseif ($url != '') {
        $paramjs = 'window.location.href =\'' . $url . '\'';
    }
    if ($paramjs) {
        $paramjs = 'function (){' . $paramjs . '}';
    } else {
        $paramjs = 'null';
    }
    $modes = array( 'error' => 'alert', 'succ' => 'succ', 'notice' => 'notice', 'js' => 'js' );
    $cover = $alert_type == 'error' ? 1 : 0;
    $extra .= 'showDialog(\'' . $message . '\', \'' . $modes[$alert_type] . '\', null, ' . ($paramjs ? $paramjs : 'null') . ', ' . $cover . ', null, null, null, null, ' . (is_numeric($time) ? $time : 'null') . ', null);';
    $extra = $extra ? '<script type="text/javascript" reload="1">' . $extra . '</script>' : '';
    if ($extrajs != '' && substr(trim($extrajs), 0, 8) != '<script>') {
        $extrajs = '<script type="text/javascript" reload="1">' . $extrajs . '</script>';
    }
    $extra .= $extrajs;
    
    ob_end_clean();
    @header("Expires: -1");
    @header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
    @header("Pragma: no-cache");
    @header("Content-type: text/xml; charset=utf-8");
    
    $string = '<?xml version="1.0" encoding="utf-8"?>' . "\r\n";
    $string .= '<root><![CDATA[' . $message . $extra . ']]></root>';
    echo $string;
    exit();
}

    // 获取树的根到子节点的路径
function getCurrentPath(&$module, $son)
{
    $path = array();
    $nav = $module[$son['parent_id']];
    $path[] = $nav['module_id'];
    if ($nav['parent_id'] > 0) {
        $path = array_merge(getCurrentPath($module, $nav), $path);
    }
    return $path;
}
